package com.example.zhangyu.myintentservice.Service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import com.example.zhangyu.myintentservice.Bean.UpdateMain;

import org.greenrobot.eventbus.EventBus;

public class MyIntentService extends IntentService {
    private final String TAG = "MyIntentService";

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public MyIntentService(String name) {
        super(name);
    }

    public MyIntentService() {
        this("MyIntentServiceThread");
    }


    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String url = intent.getStringExtra("url");
        Log.d(TAG, "onHandleIntent: " + url);
        // 模拟耗时操作，注意不用开启子线程
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
                UpdateMain updateMain = new UpdateMain();
                updateMain.setI(i);
                EventBus.getDefault().post(updateMain);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Log.d(TAG, "onHandleIntent: " + Thread.currentThread().getName());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }


}
